package com.jielihaofeng.site.category.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jielihaofeng.site.article.dao.ArticleDao;
import com.jielihaofeng.site.article.entity.ArticleEntity;
import lombok.AllArgsConstructor;
import com.jielihaofeng.framework.common.exception.FastException;
import com.jielihaofeng.framework.common.page.PageResult;
import com.jielihaofeng.framework.common.service.impl.BaseServiceImpl;
import com.jielihaofeng.framework.common.utils.TreeUtils;
import com.jielihaofeng.site.category.convert.CategoryConvert;
import com.jielihaofeng.site.category.dao.CategoryDao;
import com.jielihaofeng.site.category.entity.CategoryEntity;
import com.jielihaofeng.site.category.query.CategoryQuery;
import com.jielihaofeng.site.category.service.CategoryService;
import com.jielihaofeng.site.category.vo.CategoryVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 分类管理
 *
 * @author 借力好风 949394048@qq.com
 * @since 1.0.0 2022-07-21
 */
@Service
@AllArgsConstructor
public class CategoryServiceImpl extends BaseServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    private final ArticleDao articleDao;

    @Override
    public List<CategoryVO> getList() {
        Map<String, Object> params = new HashMap<>();

        // 分类列表
        List<CategoryEntity> entityList = baseMapper.getList(params);

        return TreeUtils.build(CategoryConvert.INSTANCE.convertList(entityList));
    }

    @Override
    public PageResult<CategoryVO> page(CategoryQuery query) {
        IPage<CategoryEntity> page = baseMapper.selectPage(getPage(query), getWrapper(query));

        return new PageResult<>(CategoryConvert.INSTANCE.convertList(page.getRecords()), page.getTotal());
    }

    private QueryWrapper<CategoryEntity> getWrapper(CategoryQuery query){
        QueryWrapper<CategoryEntity> wrapper = new QueryWrapper<>();

        return wrapper;
    }

    @Override
    public void save(CategoryVO vo) {
        CategoryEntity entity = CategoryConvert.INSTANCE.convert(vo);

        baseMapper.insert(entity);
    }

    @Override
    public void update(CategoryVO vo) {
        CategoryEntity entity = CategoryConvert.INSTANCE.convert(vo);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);
    }

    @Override
    public void delete(Long id) {
        // 判断是否有子分类
        long orgCount = count(new QueryWrapper<CategoryEntity>().eq("pid", id));
        if(orgCount > 0){
            throw new FastException("请先删除子分类");
        }

        // 判断分类下面是否有文章
        long userCount = articleDao.selectCount(new QueryWrapper<ArticleEntity>().eq("category_id", id));
        if(userCount > 0){
            throw new FastException("分类下面有文章，不能删除");
        }

        // 删除
        removeById(id);
    }

}